<template>
  <view class="login-box">
    <!-- 账号 密码 验证码 -->
    <view class="formData">
      <p>{{ _t18('login_emailCode') }}</p>
      <view>
        <input
            type="text"
            :placeholder="_t18('login_name', ['bitmake'])"
            v-model="formData1.username"
        />
      </view>
    </view>
    <view class="formData">
      <p>{{ _t18('login_pwd') }}</p>
      <view>
        <input
            :type="showk ? 'text' : 'password'"
            :placeholder="_t18('login_please')"
            v-model="formData1.password"
        /><svg-load :name="showk ? 'yanjin-k' : 'yanjin-g'" @click="showk = !showk"></svg-load>
      </view>
    </view>
    <view class="formData" v-if="mainStore.getISCode">
      <p>{{ _t18('login_code') }}</p>
      <view>
        <input type="text" :placeholder="_t18('login_please')" v-model="formData1.code" /><span
          @click="refreshCode"
      >
          <img :src="codeUrl" />
        </span>
      </view>
    </view>
    
    <p class="forgotPwd" @click="$router.push('/forgot-password')">
      {{ _t18('forget_pwd', ['bitmake']) }}
    </p>
    <p class="forgotPwd" @click="handleAppDownload">
      立即下载
    </p>
    <Footer :type="0" :formDataToLogin="formData1" @refersh="refreshCode"></Footer>
  </view>
</template>

<script setup>
import Footer from './../signFooter.vue'
import { _t18 } from '@/utils/public'
import { useMainStore } from '@/store/index.js'
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Toast } from 'vant' // 如果已安装vant

// 新增下载链接配置
const downloadUrl = ref("http://download.app.bitark.cc/") // 替换为实际下载链接
// 处理下载按钮点击
const handleAppDownload = async () => {
  try {
    // 1. 复制链接到剪贴板
    await navigator.clipboard.writeText(downloadUrl.value)

    // 2. 显示提示（根据环境适配）
    if (isInApp()) {
      // App内提示
      Toast({
        message: '下载链接已复制，请粘贴到浏览器打开',
        duration: 3000
      })

      // 可选：3秒后自动跳转浏览器（需要配置）
      // setTimeout(() => {
      //   window.location.href = downloadUrl.value
      // }, 3000)
    } else {
      // 浏览器环境提示
      Toast.success('链接已复制，请粘贴到地址栏下载')
    }
  } catch (err) {
    // 降级方案：手动复制提示
    fallbackCopy(downloadUrl.value)
  }
}
// 检测是否在App内
const isInApp = () => {
  return /(Android|iPhone|iPad).*Version\/\d+.*Mobile\/\w+/.test(navigator.userAgent)
}
// 兼容旧浏览器的复制方案
const fallbackCopy = (text) => {
  const input = document.createElement('input')
  input.value = text
  document.body.appendChild(input)
  input.select()

  try {
    document.execCommand('copy')
    Toast('请手动粘贴链接到浏览器: ' + text)
  } catch {
    // 终极降级：直接跳转
    window.open(downloadUrl.value, '_blank')
  } finally {
    document.body.removeChild(input)
  }
}
const mainStore = useMainStore()
const formData1 = ref({
  type: 1, //自定义注册类型：1普通2邮箱3手机
  username: '', //用户名/邮箱/手机号
  password: '', //密码
  code: '' //验证码
})
// ***************刷新验证码**************
// 时间戳
const timestamp = ref(+new Date())
// 图形验证码地址
const codeUrl = computed(
    () => `${mainStore.verificationCodeUrl}?codeType=LOGIN&timestamp=${timestamp.value}`
)

// 刷新验证码
const refreshCode = () => {
  timestamp.value = +new Date()
}
// 眼睛
const showk = ref(false)

import './../style.scss'
</script>

<style lang="scss" scoped>
  .forgotPwd{
    display: inline-block;
    margin-bottom: 15px;
    margin-top: 0;
    margin-right: 15px;
    text-align: right;
    
  }
</style>
